/**
 * 
 */
package com.edocbase.biz.diary.entities;

import java.util.ArrayList;
import java.util.List;

import com.edocbase.entity.EntityManager;
import com.edocbase.entity.EntityManagerFactory;
import com.edocbase.entity.StoreException;

/**
 * @author lifei114@gmail.com
 *
 */
public class SearchWordManager {
	private static SearchWordManager manager = null;
	private EntityManager<SearchWord> wordManager = null;
	
	public SearchWordManager() {
		super();
		wordManager = EntityManagerFactory.getEntityManager(SearchWord.class);
	}
	public static SearchWordManager getManager(){
		if (manager == null){
			manager = new SearchWordManager();
		}
		return manager;
	}
	public void addWord(String word){
		try {
			SearchWord searchWord = wordManager.get(word);
			if (searchWord == null){
				searchWord = new SearchWord(word);
			}
			searchWord.setUseTimes(searchWord.getUseTimes()+1);
			wordManager.save(searchWord);
		} catch (StoreException e) {
			e.printStackTrace();
		}
	}
	
	public List<SearchWord> listWords(){
		try {
			return wordManager.createQuery().OrderBy("DESC", "useTimes").executeQuery();
		} catch (StoreException e) {
			e.printStackTrace();
		}
		return new ArrayList<SearchWord>();
	}
}
